<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.accidentId ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item label="事故标题" prop="title">
          <el-input v-model="dataForm.title" placeholder="事故标题"></el-input>
      </el-form-item>
          <el-form-item label="事故描述" prop="describss">
          <el-input v-model="dataForm.describss" placeholder="事故描述"></el-input>
      </el-form-item>
          <el-form-item label="事故正确处理方式" prop="correctHandling">
          <el-input v-model="dataForm.correctHandling" placeholder="事故正确处理方式"></el-input>
      </el-form-item>
          <el-form-item label="事故图片" prop="picture">
         <el-upload
            class="upload-demo"
            :action="url"
            multiple
            :before-upload="beforeUploadHandle"
            :on-success="successHandle"
            :file-list="fileList2"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
      </el-form-item>
          <el-form-item label="事故违反的安规条例" prop="violation">
          <el-input v-model="dataForm.violation" placeholder="事故违反的安规条例"></el-input>
      </el-form-item>
          <el-form-item prop="type" label="所属专业">
        <ren-radio-group v-model="dataForm.type" dict-type="type"></ren-radio-group>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import Cookies from 'js-cookie'
export default {
  data () {
    return {
      url: '',
      num: 0,
      fileList2: [],
      visible: false,
      dataForm: {
        accidentId: '',
        title: '',
        describss: '',
        correctHandling: '',
        picture: '',
        violation: '',
        type: '',
        createDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        title: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        describss: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        correctHandling: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        picture: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        violation: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        type: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.url = `${window.SITE_CONFIG['apiURL']}/sys/oss/upload?token=${Cookies.get('token')}`
      this.num = 0
      this.fileList = []
      this.fileList2 = []
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.accidentId) {
          this.getInfo()
        }
      })
    },
    // 阻止upload的自己上传，进行再操作
    beforeUploadHandle (file) {
      this.fileList = []
      if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
        this.$message.error(this.$t('upload.tip', { 'format': 'jpg、png、gif' }))
        return false
      }
      this.num++
    },
    Close () {
      this.visible = false
      this.fileList = []
      this.fileList2 = []
    },
    handleClose (done) {
      done()
      this.fileList = []
      this.fileList2 = []
    },
    // 上传成功
    successHandle (res, file, fileList) {
      if (res.code !== 0) {
        return this.$message.error(res.msg)
      }
      this.fileList = []
      this.fileList = fileList.pop()
      this.fileList2 = [{ name: this.fileList.name, url: this.fileList.response.data.src }]
      console.log(fileList)
      this.dataForm.picture = this.fileList.response.data.src
      this.num--
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/exam/accident/${this.dataForm.accidentId}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.fileList2 = [{ name: '1.jpeg', url: this.dataForm.picture }]
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.accidentId ? 'post' : 'put']('/exam/accident/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
